//
//  NextViewController.m
//  AutoLayout
//
//  Created by yangjw  on 13-5-31.
//  Copyright (c) 2013年 yangjw . All rights reserved.
//

#import "NextViewController.h"
#import "CustomViewController.h"

@interface NextViewController ()
{
	UIButton *firstButton;
	UIButton *secondButton;

}
@end

@implementation NextViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
/*
	firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [firstButton setTitle:@"First" forState:UIControlStateNormal];
    [firstButton sizeToFit];
    firstButton.translatesAutoresizingMaskIntoConstraints = NO;
	
    [self.view addSubview:firstButton];
//	先对其按钮的左边和self.view左边的距离
//view1.attribute = view2.attribute * multiplier + constant
	NSLayoutConstraint *constraint = [NSLayoutConstraint
									  constraintWithItem:firstButton
									  attribute:NSLayoutAttributeLeading
									  relatedBy:NSLayoutRelationEqual
									  toItem:self.view
									  attribute:NSLayoutAttributeLeading
									  multiplier:1.0f
									  constant:20.f];
	
    [self.view addConstraint:constraint];
	constraint = [NSLayoutConstraint
				  constraintWithItem:firstButton
				  attribute:NSLayoutAttributeTop
				  relatedBy:NSLayoutRelationEqual
				  toItem:self.view
				  attribute:NSLayoutAttributeTop
				  multiplier:1.0f
                  constant:30.f];
	
    [self.view addConstraint:constraint];
	
	secondButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[secondButton setTitle:@"Second" forState:UIControlStateNormal];
	[secondButton sizeToFit];
//	去除限制视图编译的约素 autosizing
	secondButton.translatesAutoresizingMaskIntoConstraints = NO;
	
	[self.view addSubview:secondButton];
	
	
	constraint = [NSLayoutConstraint
                  constraintWithItem:secondButton
                  attribute:NSLayoutAttributeBottom
                  relatedBy:NSLayoutRelationEqual
                  toItem:self.view
                  attribute:NSLayoutAttributeBottom
                  multiplier:1.0f
                  constant:-40.f];
	
	[self.view addConstraint:constraint];
	
	constraint = [NSLayoutConstraint
                  constraintWithItem:secondButton
                  attribute:NSLayoutAttributeCenterX
                  relatedBy:NSLayoutRelationEqual
                  toItem:self.view
                  attribute:NSLayoutAttributeCenterX
                  multiplier:1.0f
                  constant:0.0f];
	
	[self.view addConstraint:constraint];
	
	constraint = [NSLayoutConstraint
				  constraintWithItem:secondButton
				  attribute:NSLayoutAttributeWidth
				  relatedBy:NSLayoutRelationEqual
                  toItem: nil
				  attribute:NSLayoutAttributeNotAnAttribute
				  multiplier:1.0f
				  constant:200.0f];
	
	[self.view addConstraint:constraint];
//	删除AutoLayout
//	[secondButton removeConstraint:constraint];
*/
	
/*
 [NSLayoutConstraint constraintsWithVisualFormat:<visual format string>
										options:<options>
                                        metrics:<metrics>
										views: <views dictionary>
 ];
 constraintsWithVisualFormat:参数为NSString型，指定Contsraint的属性，是垂直方向的限定还是水平方向的限定，参数定义一般如下：
 V:|-(>=XXX) :表示垂直方向上相对于SuperView大于、等于、小于某个距离
 若是要定义水平方向，则将V：改成H：即可
 在接着后面-[]中括号里面对当前的View/控件 的高度/宽度进行设定；
 options：字典类型的值；这里的值一般在系统定义的一个enum里面选取
 metrics：nil；一般为nil ，参数类型为NSDictionary，从外部传入 //衡量标准
 views：就是上面所加入到NSDictionary中的绑定的View
 在这里要注意的是 AddConstraints  和 AddConstraint 之间的区别，一个添加的参数是NSArray，一个是NSLayoutConstraint
 
 */
/*
	  |: 表示父视图
	  -:表示距离
	  V:  :表示垂直
	  H:  :表示水平
	>= :表示视图间距、宽度和高度必须大于或等于某个值
    <= :表示视图间距、宽度和高度必须小宇或等于某个值
    == :表示视图间距、宽度或者高度必须等于某个值
	@  :>=、<=、==  限制   最大为  1000
 
	1.|-[view]-|:  视图处在父视图的左右边缘内
	2.|-[view]  :   视图处在父视图的左边缘
	3.|[view]   :   视图和父视图左边对齐
	4.-[view]-  :  设置视图的宽度高度
	5.|-30.0-[view]-30.0-|:  表示离父视图 左右间距  30
	6.[view(200.0)] : 表示视图宽度为 200.0
	7.|-[view(view1)]-[view1]-| :表示视图宽度一样，并且在父视图左右边缘内
	8. V:|-[view(50.0)] : 视图高度为  50
	9: V:|-(==padding)-[imageView]->=0-[button]-(==padding)-| : 表示离父视图的距离
							为Padding,这两个视图间距必须大于或等于0并且距离底部父视图为 padding。
	10:  [wideView(>=60@700)]  :视图的宽度为至少为60 不能超过  700
	11: 如果没有声明方向默认为  水平  V:
 */
	
	// Get a reference to the superview
	UIView *superview = self.view;
	
	//Create a label
	UILabel *mylabel = [[UILabel alloc]init];
	[mylabel setTranslatesAutoresizingMaskIntoConstraints:NO];//取消自动布局autoresizingMask
	mylabel.text = @"My Label";
	
	//Create a button
	UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[mybutton setTitle:@"My Button" forState:UIControlStateNormal];
	[mybutton setTranslatesAutoresizingMaskIntoConstraints:NO];//取消自动布局autoresizingMask
	
	//Add the button and label to the superview
	[superview addSubview:mylabel];
	[superview addSubview:mybutton];
	
	// Get the views dictionary
	//NSDictionaryOfVariableBindings  宏  其实 NSDictionaryOfVariableBindings(v1, v2, v3) 等效于 [NSDictionary dictionaryWithObjectsAndKeys:v1, @"v1", v2, @"v2", v3, @"v3", nil];    
	NSDictionary *viewsDictionary =
	NSDictionaryOfVariableBindings(mylabel, mybutton);
	
	//Create the constraints using the visual language format
/*
 [cancelButton(72)]-12-[acceptButton(50)]
 1.取消按钮宽72point，accept按钮宽50point，它们之间间距12point
 
 [wideView(>=60@700)]
 2/.wideView宽度大于等于60point，该约束条件优先级为700（优先级最大值为1000，优先级越高的约束越先被满足）

 V:[redBox][yellowBox(==redBox)]
 3.竖直布局，先是一个redBox，其下方紧接一个宽度等于redBox宽度的yellowBox

 H:|-[Find]-[FindNext]-[FindField(>=20)]-|
 4.水平布局，Find距离父view左边缘默认间隔宽度，之后是FindNext距离Find间隔默认宽度；再之后是宽度不小于20的FindField，它和FindNext以及父view右边缘的间距都是默认宽度。（竖线'|‘ 表示superview的边缘）
 
 5.默认是水平布局
	H:|-[mybutton]-[mylabel(==mybutton)]-|
 */
	NSArray *constraintsArray = [NSLayoutConstraint
								 constraintsWithVisualFormat:@"H:|-20-[mybutton]-30-[mylabel]-20-|"
								 options:NSLayoutFormatAlignAllBaseline metrics:nil
								 views:viewsDictionary];
	
	//Work through the array of constraints, applying each to the superview
	for (int i = 0; i<constraintsArray.count; i++) {
        [superview addConstraint:constraintsArray[i]];
	}

	
	UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn1 setTitle:@"Click Me" forState:UIControlStateNormal];
    [btn1 setTranslatesAutoresizingMaskIntoConstraints:NO];//取消自动布局autoresizingMask
    [self.view addSubview:btn1];
    
    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn2 setTitle:@"Click Me Please" forState:UIControlStateNormal];
    [btn2 setTranslatesAutoresizingMaskIntoConstraints:NO];//取消自动布局autoresizingMask
    [self.view addSubview:btn2];
    
    NSDictionary *views=NSDictionaryOfVariableBindings(btn1,btn2);
    [self.view addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(==50)-[btn1(100)]"
                                             options:0
                                             metrics:nil
                                               views:views]];
    [self.view addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(==50)-[btn1(30)]"
                                             options:0
                                             metrics:nil
                                               views:views]];
    
    [self.view addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"H:[btn2(==150)]"//H=Horizontal，水平方向，同时设定控件宽度
											 options:0
                                             metrics:nil
                                               views:views]];
    [self.view addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=200)-[btn2(==btn1)]" //V=Vertical,垂直方向，同时设定控件高度
                                             options:0
                                             metrics:nil
                                               views:views] ];
    
    
    //注意AddConstraints和AddConstraint之间的区别，一个添加的参数是(NSArray *)，一个是(NSLayoutConstraint *)
//	btn2.attribute = btn1.attribute * multiplier + constant
    [self.view addConstraint:
     [NSLayoutConstraint constraintWithItem:btn2
                                  attribute:NSLayoutAttributeLeft
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:btn1
                                  attribute:NSLayoutAttributeRight
                                 multiplier:1
                                   constant:10]];
    [self.view addConstraint:
//	 btn2.attribute = btn1.attribute*multiplier +constant
     [NSLayoutConstraint constraintWithItem:btn2
                                  attribute:NSLayoutAttributeTop  //要设定的属性
                                  relatedBy:NSLayoutRelationGreaterThanOrEqual  //大于还是小于相对的View的值
                                     toItem:btn1   //相对于某个View或者控件
                                  attribute:NSLayoutAttributeTop  //指定要设定的关联View的属性
                                 multiplier:1   //因子值
                                   constant:0]];
    
	
    [super viewDidLoad];
	
//	[self.view layoutIfNeeded];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)back:(id)sender
{
	CustomViewController *custom = [[CustomViewController alloc] initWithNibName:@"CustomViewController" bundle:nil];
	[self presentViewController:custom animated:YES completion:nil];
}
@end
